/* mouse right click event */

var SystemTemplateUtil = {
	escape:function(query){
		if(query){
			var q = query.replace(/'/g,"&acute;");
			q = q.replace(/´/g,"&acute;");
			q = q.replace(/"/g,"&quot;");
			return q;
		}else{
			return "";
		}
	},
	descape:function(query){
		if(query){
			var q = query.replace(/&acute;/g,"'");
			q = q.replace(/&quot;/g,'"');
			return q;
		}else{
			return "";
		}
	},
	isTag:function (target,name){//target is html document,name is a html tagName string,such as "li" "a"
		var nameUpcase = name.toUpperCase();
		var tagName = target.tagName;
		if(tagName == name ||tagName == nameUpcase){
			return true;
		}
		return false;
	},
	genelateAce:function(targetId,templateId,data){
		if(data){
			if((data instanceof Object)&&$.isEmptyObject(data))
				return false;
			if((data instanceof Array)&&data.length==0)
				return false;
			var html = AceTemplate.format(templateId,data,{
					dateIntercept:function(t){
						return t.substring(0,10);
					},
					isNull:function(value){
						if(value == undefined||value == null||$.trim(value)==""){
							return false;
						}
						return true;
					}
			});
			$("#"+targetId).append(html);
		}
	},
	genelateAceValue:function(templateId,data){
		if(data){
			if((data instanceof Object)&&$.isEmptyObject(data))
				return false;
			if((data instanceof Array)&&data.length==0)
				return false;
			var html = AceTemplate.format(templateId,data,{
					dateIntercept:function(t){
						return t.substring(0,10);
					},
					isNull:function(value){
						if(value == undefined||value == null||$.trim(value)==""){
							return false;
						}
						return true;
					},
					genelateSelect:function(v,id){
						var ids = id.split(",");
						return $.inArray(v+"",ids)>=0?"selected='selected'":"";
					},
					getKeywords:function(v){
						return Util.replaceQuot(v);
					},
					genelateSelectValue:function(v,name,id){
						var ids = id.split(",");
						//return $.inArray(v+"",ids)>=0?"selected='selected'":"";
						return $.inArray(v+"",ids)>=0?name+',':'';
					}
					
			});
			return html;
		}
		return "";
	},
	generatePages:function(totalPageSize,currentPageNO,showSize){
		if(totalPageSize<=1){
			return "";
		}
		if(showSize == undefined){
			showSize = 5;
		}
		function gPageList(cur,index){
			if(cur == index){
				return "<span class='current'>"+cur+"</span>";
			}else{
				return "<span class='nomal'>"+index+"</span>";
			}
		}
		var half = Math.floor(showSize/2);
		var phtml = "";
		if(currentPageNO>1){//if has prev
			phtml +="<span class='prev'></span>";
		}
		if(totalPageSize<=(showSize+2)){//can show totalPages
			for(var i=1;i<=totalPageSize;i++){
				phtml += gPageList(currentPageNO,i);
			}
		}else{//show part
			phtml += gPageList(currentPageNO,1);
			if(currentPageNO-half<=1){
				for(var i=2;i<2+showSize;i++){//pages concat with 1
					phtml += gPageList(currentPageNO,i);
				}
				phtml += "... ";
			}else if(currentPageNO+half+1>=totalPageSize){//at center
				phtml += "... ";
				for(var i=totalPageSize-showSize;i<totalPageSize;i++){
					phtml += gPageList(currentPageNO,i);
				}
			}else{
				phtml += "... ";
				for(var i=currentPageNO-half;i<=currentPageNO+half;i++){//pages concat totalPageSize
					phtml += gPageList(currentPageNO,i);
				}
				phtml += "... ";
			}
			phtml +=gPageList(currentPageNO,totalPageSize);;
		}
		if(currentPageNO<totalPageSize){//if has next
			phtml +="<span class='next'></span>";
		}
		return phtml;
	},
	initPagesclick:function(tagId,func,param){
		$("#"+tagId).unbind("click").click(function(e){
			var ntag = e.target;
			if(eagleEyeUtil.isTag(ntag,"span")){
				var qtag = $(ntag);
				var nindex = parseInt($("#"+tagId+" .current").html());
				if(qtag.hasClass("nomal")){
					func(parseInt(qtag.html()),param);
				}else if(qtag.hasClass("prev")){
					func(nindex-1,param);
				}else if(qtag.hasClass("next")){
					func(nindex+1,param);
				}
			}
		});
	},
	genelateSplit:function(count){
		var res = "\n";
		for(var i=0;i<count;i++){
			res += ",";
		}
		return res;
	}
};

DateUtil = {
	stringToDate:function(time){
		var ts = time.split("-");
		var year = ts[0];
		var month = parseInt(ts[1],10)-1;
		var day = ts[2];
		return new Date(year,month,day);
	},
	dataAdd:function(date,days){
		if(typeof(date) == "string"){
			date = $.trim(date);
			date = this.stringToDate(date);
		}
		var tempdate = new Date(date.getTime());
		tempdate.setDate(tempdate.getDate()+days);
		return tempdate;
	},
	dateToString:function(date){
		if(date){
			return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
		}
	}
};
/* 生成dialog的参数 */
function d(name,sname){
	if(sname==undefined||sname==null){
		sname = name;
	}
	return {query:name,displayname:sname};
}


